# Maintainer: Phillip Smith <fukawi2@NO-SPAM.gmail.com>
# https://github.com/fukawi2/aur-packages
# Contributor: Piotr Beling <qwak@w8.pl>

pkgname=awstats
pkgver=7.9
pkgrel=1
pkgdesc="tool that generates web, streaming, ftp and mail server statistics, graphically"
arch=('any')
license=('GPL')
url="http://awstats.sourceforge.net/"
depends=('perl')
backup=('etc/awstats/awstats.model.conf'
	'etc/httpd/conf/extra/httpd-awstats.conf')
#source=("http://www.awstats.org/files/${pkgname}-${pkgver}.tar.gz"
source=("https://downloads.sourceforge.net/${pkgname}/${pkgname}-${pkgver}.tar.gz"
#        "awstats-7.8_fix-CVE-2020-35176.patch::https://github.com/eldy/AWStats/commit/0d4d4c05f8e73be8f71dd361dc55cbd52858b823.patch"
        "cron.awstats")
sha256sums=('615178ed313d34315f15a522db1a5d12ca9c395e3785bb06280abff95d9a0546'
            '18e2a1568da87540e8331982fc10f63f7c2ad8ba8fc593f3219469c6ecdef5d2')

prepare() {
  cd "$pkgname-$pkgver"
#  patch -Np1 --no-backup-if-mismatch -i "$srcdir/awstats-7.8_fix-CVE-2020-35176.patch"
}

package() {
  _SYSCONFDIR="etc"
  _WWWROOT="usr/share/webapps/${pkgname}"
  _INSTDIR="usr/share/${pkgname}"
  _DOCDIR="usr/share/doc/${pkgname}-${pkgver}"

  cd "$srcdir"/${pkgname}-${pkgver}

  mkdir -p "$pkgdir"/${_INSTDIR}/tools/webmin/
  mkdir -p "$pkgdir"/${_INSTDIR}/tools/xslt/
  mkdir -p "$pkgdir"/${_WWWROOT}/
  mkdir -p "$pkgdir"/${_DOCDIR}/
  mkdir -p "$pkgdir"/var/lib/$pkgname

  # Arch Linux specific path:
  install -Dm644 tools/httpd_conf "$pkgdir"/${_SYSCONFDIR}/httpd/conf/extra/httpd-awstats.conf
  install -Dm644 wwwroot/cgi-bin/awstats.model.conf "$pkgdir"/${_SYSCONFDIR}/awstats/awstats.model.conf
  install -Dm644 "$srcdir"/cron.awstats "$pkgdir"/${_DOCDIR}/cron.hourly

  install -m 755 tools/logresolvemerge.pl "$pkgdir"/${_INSTDIR}/tools/logresolvemerge.pl
  install -m 755 tools/maillogconvert.pl "$pkgdir"/${_INSTDIR}/tools/maillogconvert.pl
  install -m 755 tools/urlaliasbuilder.pl "$pkgdir"/${_INSTDIR}/tools/urlaliasbuilder.pl
  install -m 755 tools/awstats_buildstaticpages.pl "$pkgdir"/${_INSTDIR}/tools/awstats_buildstaticpages.pl
  install -m 755 tools/awstats_configure.pl "$pkgdir"/${_INSTDIR}/tools/awstats_configure.pl
  install -m 755 tools/awstats_exportlib.pl "$pkgdir"/${_INSTDIR}/tools/awstats_exportlib.pl
  install -m 755 tools/awstats_updateall.pl "$pkgdir"/${_INSTDIR}/tools/awstats_updateall.pl
  install -m 755 tools/webmin/* "$pkgdir"/${_INSTDIR}/tools/webmin
  install -m 755 tools/xslt/* "$pkgdir"/${_INSTDIR}/tools/xslt

  # install the documentation
  cp -ra docs/* "$pkgdir"/${_DOCDIR}/

  # www files
  cp -ra wwwroot/* "$pkgdir"/${_WWWROOT}/
  rm -f "$pkgdir"/${_WWWROOT}/cgi-bin/awstats.model.conf
  install -Dm444 README.md "$pkgdir"/${_INSTDIR}/README.md

  # fix the default http config
  sed \
	-e "s|/usr/local/awstats/wwwroot|/${_WWWROOT}|g" \
	-i "$pkgdir"/${_SYSCONFDIR}/httpd/conf/extra/httpd-awstats.conf

  echo "deny from all" > "$pkgdir"/${_WWWROOT}/.htaccess
}
